<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" charset="UTF-8" />
    <title>For循环优化</title> 
</head> 
<body>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
<script type="text/javascript">
var aBtns = document.getElementsByClassName('btn')

for(var i = 0;i < aBtns.length;i++){
    console.log(i);
}

for(var i = 0,len = aBtns.length;i < aBtns.length;i++){//不需要像上面代码一样，每次都需要重新获取长度
    console.log(i)
}
</script> 
</body> 
</html>